package com.cjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjh.common.R;
import com.cjh.dto.OrderDto;
import com.cjh.entity.OrderDetail;
import com.cjh.entity.Orders;
import com.cjh.service.OrderDetailService;
import com.cjh.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrdersService ordersService;

    @Resource
    private OrderDetailService orderDetailService;

    @GetMapping("/page")
    public R<Page<Orders>> page(Integer page, Integer pageSize,
                                Long id, String beginTime, String endTime) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(id != null, Orders::getId, id);
        wrapper.between(beginTime != null || endTime != null, Orders::getOrderTime, beginTime, endTime);
        wrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo, wrapper);

        return R.success(pageInfo);
    }


    @GetMapping("/userPage")
    public R<Page<OrderDto>> userPage(Integer page, Integer pageSize) {
        //获取orders信息
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrderDto> dtoPage = new Page<>(page, pageSize);

        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Orders::getId);
        Page<Orders> pageInfo = ordersService.page(ordersPage, wrapper);
        //将orders中的信息拷贝到dto集合中

        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Orders> records = pageInfo.getRecords();
        List<OrderDto> collect = records.stream().map(item -> {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(item, orderDto);
            LambdaQueryWrapper<OrderDetail> orderDetailWrapper = new LambdaQueryWrapper<>();
            orderDetailWrapper.eq(OrderDetail::getOrderId, orderDto.getId());
            orderDto.setOrderDetails(orderDetailService.list(orderDetailWrapper));

            return orderDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(collect);
        return R.success(dtoPage);
    }

    @PutMapping
    public R<String> put(@RequestBody Orders orders) {
        Orders order = ordersService.getById(orders.getId());
        order.setStatus(orders.getStatus());
        ordersService.updateById(order);
        return R.success("开始派送");
    }


    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {

        ordersService.submit(orders);
        return R.success("下单成功");
    }
}
